#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int distant(string str1, string str2, vector<string> vs)
{
	int ret = INT_MAX;
	int p1 = -1, p2 = -1;

	for (int i=0;i<vs.size();i++)
	{
		if (str1 == vs[i])
			p1 = i;
		if (str2 == vs[i])
			p2 = i;

		if (p1!=-1 && p2!=-1)
			ret = min(ret, abs(p1 - p2));
	}

	if (ret == INT_MAX)
		return -1;
	else
		return ret;
}

int main()
{
	string str1, str2;
	cin >> str1 >> str2;

	int n;
	cin >> n;
	vector<string> vs(n);
	for (int i = 0; i < n; i++)
		cin >> vs[i];

	cout << distant(str1, str2, vs) << endl;

	return 0;
}